using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace ApplicationLibrary.ExtendedControls
{
    [Designer(typeof(MySnapLinesDesigner))]
    public partial class CalendarTextBox : UserControl, ISnapLineControl
    {
        protected System.Windows.Forms.DateTextBox textBox1;
        private System.Windows.Forms.Button button1;

        public Control textBoxValue { get { return textBox1; } }

        public Control labelValue { get { return null; } }


        PopUpCalendar popup = null;
        /// <summary>
        /// Required designer variable.
        /// </summary>

        #region EventHandler

        new public event EventHandler GotFocus;

        new public event EventHandler LostFocus;

        public event EventHandler PageUp;

        public event EventHandler PageDown;

        #endregion

        #region Public Properties

        public DateTime EmptyDate { get { return new DateTime(1799, 12, 31); } }

        public override string Text
        {
            get
            {
                return textBox1.Text.Equals("")
                  ? "1799/12/31"
                  : textBox1.Text;
            }
            set
            {
                DateTime dt;
                DateTime.TryParse(value, out dt);
                DateTime empty = new DateTime(1799, 12, 31);
                if (value != null)
                {
                    textBox1.Text = value == null || dt.Equals(empty)
                      ? "" :
                      value;
                }
                else
                    textBox1.Text = "";
            }
        }

        public TextBox rdrTxtBox { get { return textBox1; } }

        public int MaxLength { set { textBox1.MaxLength = value; } }

        public bool IsInDataGrid
        {
            set { textBox1.IsInDataGrid = value; }
            get { return textBox1.IsInDataGrid; }
        }

        public DateTime Today
        {
            set
            {
                try
                {
                    if (textBox1 != null && value != null)
                        textBox1.SetDate(value);
                }
                catch
                {
                    textBox1.SetDate(1799, 12, 31);
                }
            }
            get
            {
                try
                {
                    return textBox1.GetDate();
                }
                catch
                {
                    return new DateTime(1799, 12, 31);
                }
            }
        }

        public BorderStyle rdrBorderStyle
        {
            set { textBox1.BorderStyle = value; }
            get { return textBox1.BorderStyle; }
        }

        public int SelectionStart
        {
            get { return textBox1.SelectionStart; }
            set { textBox1.SelectionStart = value; }
        }

        public int SelectionLength
        {
            get { return textBox1.SelectionLength; }
            set { textBox1.SelectionLength = value; }
        }

        #endregion

        public CalendarTextBox()
            : base()
        {
            InitializeComponent();
            textBox1.ShowDayBeforeMonth = true;
        }

        private void CalendarTextBox_Resize(object sender, System.EventArgs e)
        {
            button1.Width = textBox1.Height;
            button1.Height = textBox1.Height;
            button1.Left = this.Size.Width - button1.Width;
            button1.Top = textBox1.Top;
            textBox1.Width = this.Width - button1.Width;
            this.Height = textBox1.Height;
        }

        private void CalendarTextBox_Paint(object sender, PaintEventArgs e)
        {
            button1.Width = textBox1.Height;
            button1.Height = textBox1.Height;
            button1.Left = this.Size.Width - button1.Width;
            button1.Top = textBox1.Top;
            textBox1.Width = this.Width - button1.Width;
            button1.Font = new Font("Webdings", this.Font.GetHeight() - this.Font.GetHeight() / 4);
            this.Height = textBox1.Height;
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.F10)
                ApriCalendar();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ApriCalendar();
        }

        private void ApriCalendar()
        {
            if (textBox1.Text == string.Empty || !textBox1.IsValid())
                textBox1.Text = DateTime.Now.ToShortDateString();

            popup = new PopUpCalendar(Convert.ToDateTime(textBox1.Text));
            popup.AfterDateSelectEvent += new PopUpCalendar.AfterDateSelectedSelectEventHandler(popup_AfterDateSelectEvent);
            popup.Location = location();
            popup.ShowDialog();
        }

        private void popup_AfterDateSelectEvent(object sender, DateTime SelectedDate)
        {
            textBox1.Focus();
            textBox1.SetDate(SelectedDate.Year, SelectedDate.Month, SelectedDate.Day);
            popup.Close();
        }

        private Point location()
        {
            int X = this.PointToScreen(textBox1.Location).X;
            int Y = this.PointToScreen(textBox1.Location).Y + textBox1.Size.Height;

            return new Point(X, Y);
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
        }

        private void popup_AfterRowSelectEvent(object sender, string SelectedValue)
        {
            textBox1.Focus();
            textBox1.Text = SelectedValue;
        }

        #region Raise Event

        private void button1_GotFocus(object sender, EventArgs e)
        {
            textBox1.Focus();
        }

        private void textBox1_GotFocus(object sender, EventArgs e)
        {
            if (this.GotFocus != null)
                this.GotFocus(this, e);
        }

        private void textBox1_LostFocus(object sender, EventArgs e)
        {
            if (this.LostFocus != null)
                this.LostFocus(this, e);
        }

        private void button1_LostFocus(object sender, EventArgs e)
        {
            if (this.LostFocus != null)
                this.LostFocus(this, e);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            this.OnTextChanged(e);
        }

        private void textBox1_PageDown(object sender, EventArgs e)
        {
            if (this.PageDown != null)
                this.PageDown(this, e);
        }

        private void textBox1_PageUp(object sender, EventArgs e)
        {
            if (this.PageUp != null)
                this.PageUp(this, e);
        }

        #endregion

        private void LookUpComboBox_GotFocus(object sender, EventArgs e)
        {
            this.textBox1.Focus();
            if (popup!= null)
                popup.Close();

            popup = null;
        }
    }
}